<template>
	<view class="container">
		<uni-group mode="card" :is-shadow="false" :title="this.dealFormData.nodeName">
			<current-form :formJson="formJson" :rules="rules" :modelValue="dealFormData" :showReject="false"></current-form>
		</uni-group>
	</view>
</template>

<script>
	import CurrentForm from '../components/currentForm.vue'
	import {
		getFlowBeginNodeForm,
	} from "@/utils/api/api.js"
	
	import {
		transferFormData,
	} from "@/utils/utils.js"
	
	export default {
		data() {
			return {
				// 表单数据
				dealFormData: {
					flowId: 0,
					processId: 0,
					tableName: '',
					isSave: 0,
					direction: 1,
					userId: uni.getStorageSync('userId'),
					orgId: uni.getStorageSync('orgId'),
				},
				rules: {},
				formJson: null,
			}
		},
		onLoad(options) {
			this.dealFormData.flowId = options.flowId;
			// this.dealFormData.tableName = options.tableName;
		},
		onShow() {
			
		},
		onReady() {
			this.init()
		},
		components: {
			CurrentForm
		},
		methods: {
            //第一次调用
			init() {
				if (this.dealFormData.flowId){
					let data = {
						flowId: this.dealFormData.flowId
					}
					getFlowBeginNodeForm(data).then(res => {
						console.log(res)
						if (res.code == 1) {
							if (res.data && res.data.jsonData){
							    let json = JSON.parse(res.data.jsonData);
							    this.dealFormData.tableName = res.data.tableName;
							    let widgetArr = []; // 表单组件数组，不包含grid、table、tab、card等组件
							    // 数据格式处理
							    transferFormData({}, json.widgetList, widgetArr);
							    this.formJson = widgetArr;
							    this.formJson.forEach((s) => {
							    	this.dealFormData[s.options.name] = s.options.defaultValue;
							    	this.rules[s.options.name] = {
							    		rules: []
							    	}
							    	if (s.options.required){
							    		this.rules[s.options.name].rules.push({
							    			required: true,
							    			errorMessage: '不能为空'
							    		})
							    	}
							    })
							}
						} else {
							uni.showToast({
								title: res.msg,
								duration: 2000,
								mask: true,
								icon: 'none'
							});
						}
					})
				}
			},
		}
	}
</script>

<style>
	.container {
		font-size: 14px;
		line-height: 24px;
	}
</style>
